﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using VirtualPress.Tool.Framework;

namespace VirtualPress.Infrastructure.Entities.Repository
{
    [Table("T_News")]
    public class T_News : IEntity<T_News>
    {
        [Column(TypeName="int")]
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }

        [Column(TypeName="nvarchar")]
        [StringLength(128)]
        public string Title { get; set; }

        [Column(TypeName = "text")]
        public string Content { get; set; }

        [Column(TypeName="nvarchar")]
        [StringLength(64)]
        public string Editor { get; set; }

        /// <summary>
        /// 展示责任编辑
        /// </summary>
        [Column(TypeName="nvarchar")]
        [StringLength(64)]
        public string DisplayEditor { get; set; }

        /// <summary>
        /// 摘录地址
        /// </summary>
        [Column(TypeName="nvarchar")]
        [StringLength(64)]
        public string Form { get; set; }
        
        /// <summary>
        /// 分类
        /// 0：站点新闻 1：期刊新闻 2：会议新闻
        /// </summary>
        public int Type { get; set; }

        public int OrgType { get; set; }
        public int OrgId { get; set; }
        /// <summary>
        /// 创建时间
        /// </summary>
        [Column(TypeName="datetime")]
        public DateTime CreateDate { get; set; }

        public override T_News Find(System.Linq.Expressions.Expression<Func<T_News, bool>> where)
        {
            using (Context ctx = new Context())
            {
                var item = ctx.News.FirstOrDefault(where);
                if (item != null)
                {
                    this.Id = item.Id;
                    this.Title = item.Title;
                    this.Content = item.Content;
                    this.Editor = item.Editor;
                    this.DisplayEditor = item.DisplayEditor;
                    this.Form = item.Form;
                    this.CreateDate = item.CreateDate;
                    this.Type = item.Type;
                    this.OrgId = item.OrgId;
                    this.OrgType = item.OrgType;
                    return this;
                }
                return null;
            }
        }

        public override T_News Find(string key)
        {
            int id = key.ToInt32();
            return Find(c => c.Id == id);
        }

        public override bool Any(System.Linq.Expressions.Expression<Func<T_News, bool>> where)
        {
            using (Context ctx = new Context())
            {
                return ctx.News.Any(where);
            }
        }

    }
}
